package com.eece496.Server;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.GroupInfo;

/**
 * Servlet implementation class ProfEditGroupServlet
 */
public class ProfEditGroupServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfEditGroupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String String1 = request.getParameter("radio");
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String Message = "Success";
		System.out.println(String1);
		String sID = String1.substring(0, 8);
		String gNum = String1.substring(9, String1.length());
		
		int studentID = Integer.parseInt(sID);
		int groupNumber = Integer.parseInt(gNum);
		
		System.out.println(sID);
		System.out.println(gNum);
		
		StudentGroups StudentGroupDB = new StudentGroups();
		StudentGroupDB.Setup(connection);
		TAduties TADutyDB = new TAduties();
		TADutyDB.Setup(connection);
		
		try {
			
			if(TADutyDB.UserExists(studentID, courseNumber, sectionNumber)==true && TADutyDB.getSize() > 0)
			{
				Message = "Failed";
			}
			else{
			StudentGroupDB.DeleteStudentFromGroup(studentID, courseNumber, sectionNumber);
			}
			ArrayList<GroupInfo> Info = new ArrayList<GroupInfo>();

			Info = StudentGroupDB.GetAllGroups(courseNumber, sectionNumber);

			//Get all students that aren't grouped up
			ArrayList<GroupInfo> unGroupedInfo = new ArrayList<GroupInfo>();
			ArrayList<GroupInfo> gInfo = new ArrayList<GroupInfo>();
			unGroupedInfo = StudentGroupDB.GetUnGroupedStudents(courseNumber, sectionNumber);
			int count =0;
			for(int i=0; i< unGroupedInfo.size(); i++)
			{
				if(StudentGroupDB.UserExists(courseNumber, sectionNumber, unGroupedInfo.get(i).studentID)==false)
				{
					gInfo.add(unGroupedInfo.get(i));
					System.out.println(count);
				}
			}
			
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("GroupsInfo", Info);
			request.setAttribute("unGroupedInfo", gInfo);
			request.setAttribute("Message", Message);
			request.getRequestDispatcher(
					"/Professor/Professor-View-Groups.jsp").forward(
					request, response);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}
